﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace game
{
    class opr_game1
    {
        public string sql;
        /*
         * 增加操作
         * 参数：无
         * 返回值：conn
         */
        public void game1_insert(ref SqlConnection conn)
        {
            Console.WriteLine("游戏名字");
            string nameGame = Console.ReadLine();
            Console.WriteLine("游戏价钱");
            int PriceGame = int.Parse(Console.ReadLine());
            Console.WriteLine("游戏库存");
            int stockGame = int.Parse(Console.ReadLine());
            sql = string.Format("insert into game values('{0}','{1}','{2}')", nameGame, PriceGame, stockGame);
            SqlCommand cod = new SqlCommand(sql, conn);
            if (cod.ExecuteNonQuery() > 0)
            {
                Console.WriteLine("操作成功");
            }
            else
            {
                Console.WriteLine("操作失败");
            }
        }
        /*
         * 查询操作
         * 参数：无
         * 返回值：conn
         */
        public void game1_select(ref SqlConnection conn)
        {
            Console.WriteLine("======================================查找页面======================================");
            sql = "select IDgame,nameGame,PriceGame,stockGame,PriceGame*stockGame as Subtotal from game";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            Console.WriteLine("+---------------------------------------------------------------+");
            Console.WriteLine("序号\t游戏名\t\t价钱\t库存\t小计");
            while (reader.Read())
            {
                string userID1 = reader["IDgame"].ToString();
                string pwd = reader["nameGame"].ToString();
                string name = reader["PriceGame"].ToString();
                string tel = reader["stockGame"].ToString();
                Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", userID1, pwd, name, tel, reader["Subtotal"]);
            }
            //关闭reader
            reader.Close();
            //计算总金额
            string totaSql = "select sum(PriceGame*stockGame) as sumgameprice from game";
            cmd = new SqlCommand(totaSql, conn);
            SqlDataReader reader1 = cmd.ExecuteReader();
            while (reader1.Read())
            {
                string sum1 = reader1["sumgameprice"].ToString();
                Console.WriteLine("+---------------------------------------------------------------+");
                Console.WriteLine("游戏总金额：{0}元", sum1);
            }
            
            
        }
    }
}
